/**
 * Created by lional on 16/10/3.
 */
var MainSceneController = jGame.ControllerObject.extend({

    _listData: null,

    onViewEnter: function (view) {
        this._super(view);
        this.updateMenuListView();
    },

    onViewExit: function (view) {
        this._super(view);
    },

    updateMenuListView: function () {
        var view = this.getView();
        if(view) {
            var MenuListView = jGame.findView(view, "MenuListView");
            if(MenuListView) {

                MenuListView.removeAllChildren();
                if (!this._listData)this._listData = {
                    "EmptyTest" : {"Controller" : "TipsController", "res" : "TipsLayer_json"},
                    "MVCTest" : {"Controller" : "MVCController", "res" : "MVCLayer_json"},
                    "RichTextTest": {"Controller" : "RichTextController", "res" : "RichTextLayer_json"},
                    "UIListViewTest" : {"Controller" : "UIListViewController", "res" : "UIListViewLayer_json"},
                };
                var listData = this._listData;
                var defaultItem = new jGame.Layout();
                defaultItem.setName("Layout");
                defaultItem.width = 640;
                defaultItem.height = 100;
                var button = new jGame.Button();
                button.setName("TestButton");
                button.x = 320;
                button.y = 50;
                button.anchorX = 0.5;
                button.anchorY = 0.5;
                defaultItem.addChild(button);
                MenuListView.setDefaultItemData(listData, function (id, item, index) {
                    var button = jGame.findView(item, "TestButton");
                    if(button) {
                        button.setTag(id);
                        button.setUserData(index);
                        var controller = app.getObject("MainSceneController");
                        button.setTitleText(index);
                        controller.bindClick(button, "onTestBtnClick");
                    }
                }, defaultItem);
            }
        }
    },

    onTestBtnClick: function (sender) {
        cc.log("tag : " + sender.getTag() + ", index : " + sender.getUserData());
        if(sender.getUserData() == "EmptyTest") {
            app.getRunningScene().showTips(res[this._listData[sender.getUserData()]["res"]], "Test Tips!!!");
        }else {
            app.getRunningScene().addViewWithVisibleSize(res[this._listData[sender.getUserData()]["res"]], this._listData[sender.getUserData()]["Controller"]);
        }
    }

});